package com.bilibili.liveremind.module;


import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bilibili.liveremind.module.biliwss.WebsocketListener;
import com.bilibili.liveremind.module.vo.AuthPackVo;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class DanMuInfo {

    public static void main(String[] args) throws URISyntaxException, MalformedURLException {
        //获取弹幕ws连接 https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=2322181
        String getDanMuUrl = "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo";
        int roomId = 6326729;
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",roomId);
        paramMap.put("type",0);
        String res = HttpRequest.get(getDanMuUrl).form(paramMap).execute().body();
        //System.out.println(res);
        //{"code":0,"message":"0","ttl":1,"data":{"group":"live","business_id":0,"refresh_row_factor":0.125,"refresh_rate":100,"max_delay":5000,"token":"L4lt8_PDaZn-QLaMj20YlrGnX0L6q6n5SIxkbEWuVeySJhNqZwwkzyFJ6W-3nC60B8w4T_gxExmTcYTpeQ5cnQKUTATsjPkOd3J_dY5akoHB8LldPlEmEwnEw4FNna9VzrPrR3T-_UM2WbKjC5AiEgMIeHBTrax6nLhuyjIj7FGVsdeS9g==","host_list":[{"host":"zj-cn-live-comet.chat.bilibili.com","port":2243,"wss_port":2245,"ws_port":2244},{"host":"ali-bj-live-comet-08.chat.bilibili.com","port":2243,"wss_port":2245,"ws_port":2244},{"host":"ali-gz-live-comet-02.chat.bilibili.com","port":2243,"wss_port":2245,"ws_port":2244},{"host":"broadcastlv.chat.bilibili.com","port":2243,"wss_port":2245,"ws_port":2244}]}}
        JSONObject resJson = JSONUtil.parseObj(res);
        JSONObject resDataJson = resJson.getJSONObject("data");
        String token = resDataJson.getStr("token");
        JSONArray hostListJson = resDataJson.getJSONArray("host_list");
        //System.out.println(hostListJson);
        JSONObject hostJson = hostListJson.getJSONObject(0);
        String host = hostJson.getStr("host");
        Integer port = hostJson.getInt("port");
        Integer wssPort = hostJson.getInt("wss_port");
        Integer wsPort = hostJson.getInt("ws_port");
        //创建bilibili弹幕ws连接
        String wssUrl = String.format("wss://%s:%s/sub", host,wssPort);
        AuthPackVo authPackVo = new AuthPackVo();
        authPackVo.setUid(102076113);
        authPackVo.setRoomid(6326729);
        authPackVo.setBuvid("3E8DC1D8-D75E-7470-2779-1FC4702DF90E45478infoc");
        authPackVo.setKey("ICFksvg60Ge6EsULPq8xapl7ShpCccVVsKhN7GEMWzkDADHy8iq36axNwjMehh5X_q9F9gdQpOAIB50drojtEUhatnteQGJ8h80QSQLbwmAd_n6KDqsir7WCsCjP7C8CtqEIG8PYvIjp1NTmao2hwyQ_oYoBZgisjt4ndVBZkOlGoL8GrQZ2Tes=");
        WebsocketListener websocketListener = new WebsocketListener(new URI(wssUrl),authPackVo);
        websocketListener.connect();
    }


}
